#!/bin/bash
### BEGIN INIT INFO
# Provides:          cyg.com.cn
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: redis service
# Description:       redis service daemon
### END INIT INFO
### --20220121

BASEDIR=/work/PSM70-linux-run-Standalone/init/Redis_init/../../bin/RedisServer
DATADIR=$BASEDIR/../../DataPacket/RedisData
EXEC=$BASEDIR/redis-server
CLIEXEC=$BASEDIR/redis-cli

cd $BASEDIR

CONF=$BASEDIR/redis.conf
PASSWD=$(cat ${CONF}|grep requirepass |grep '^[^#]'|awk -F" " '{print $NF}')
#REDISPORT=6379
REDISPORT=$(sed -n '/^port/p' ${CONF}|awk -F" " '{print $NF}')
# 读取端口，读不到就设置默认值6379
if test -z ${REDISPORT}
then
    REDISPORT=6379
fi

PIDFILE=$(cat ${CONF}|grep pidfile |grep '^[^#]'|awk -F" " '{print $NF}')
if test -z ${PIDFILE}; then
    PIDFILE=/var/run/redis_${REDISPORT}.pid
fi

statusServer() {
    REDIS_PID=$(ps -ef | grep "redis-server" | grep -v grep | awk -F" " '{print $2}')
    REDIS_COUNT=$(ps -ef | grep "redis-server" | grep -v grep | awk -F" " '{print $8}' | grep redis | grep -v grep | wc -l)
}

startServer() {
    statusServer
    if [ -f ${PIDFILE} ] && [ ${REDIS_COUNT} -gt 0 ];then
        echo "redis is running for(pidfile-${PIDFILE} pid-${REDIS_PID},count-${REDIS_COUNT}), you must stopped the redis server"
        exit 1
    elif [ -f ${PIDFILE} ] && [ ${REDIS_COUNT} -nq 0 ]; then
        echo "redis is exception for(pid ${REDIS_PID})........."
        for pid in ${REDIS_PID[*]};do
            echo "kill redis for pid $pid"
            kill -9 "${pid}"
        done
        if [ -f ${PIDFILE} ];then
            rm -rf ${PIDFILE}
        fi

        echo "Starting Redis server now..."
        $EXEC $CONF &
    else
        echo "Starting Redis server now..."
        $EXEC $CONF &
    fi
}
stopServer() {
    statusServer
    echo "Stopping ..."
    if [ -z "${PASSWD}" ];then
        ${CLIEXEC} -p ${REDISPORT} shutdown
    else
        ${CLIEXEC} -a ${PASSWD} -p ${REDISPORT} shutdown
    fi
    echo "redis is exception for(pid ${REDIS_PID})........."
    for i in 1 2 3
    do
        echo "Waiting for Redis to shutdown ...$i"
        sleep 1
        statusServer
        if [ "${REDIS_COUNT}" = "0" ];then
          exit 1
        fi
        echo "Redis stopped"
        for pid in ${REDIS_PID[*]};do
            echo "kill redis for pid $pid"
            kill -9 "${pid}"
        done
    done
}

case "$1" in
    start)
        startServer
        ;;
    stop)
        stopServer
        ;;
    restart)
        stopServer
        startServer
        ;;
    status)
        statusServer
        if [ -f ${PIDFILE} ] && [ ${REDIS_COUNT} -gt 0 ];then
            echo "Redis is running(pid ${REDIS_PID})"
        elif [ -f ${PIDFILE} ] && [ ${REDIS_COUNT} -eq 0 ]; then
            echo "redis is exception for(pid ${REDIS_PID})........."
        else
            echo "Redis is not running"
            echo "Redis pidfile is ${PIDFILE} pid is ${REDIS_PID}"
        fi
        ;;                       
    *)
        echo "Please use argument for( start | stop | restart | status )"
        ;;
esac
